package day05;

import java.util.Stack;


public class ComputerSuffixExpressionWithStack {
    public static void main(String[] args) {
        String[] toks = "1 2 + 3 4 + *".split("\\s+");
        Stack<Double> stack = new Stack<Double>();

        for (String tok : toks) {
            if (isNumber(tok)) {
                stack.push(Double.parseDouble(tok));
            } else {
                double b = stack.peek();
                stack.pop();
                double a = stack.peek();
                stack.pop();
                double c = 0;
                if (tok.charAt(0) == '+') {
                    c = a + b;
                } else if (tok.charAt(0) == '-') {
                    c = a - b;
                } else if (tok.charAt(0) == '*') {
                    c = a * b;
                } else if (tok.charAt(0) == '/') {
                    c = a / b;
                }
                stack.push(c);
            }


        }
        System.out.println(stack.peek());
    }

    static boolean isNumber(String str) {
        //+12.34 +1.4e-2 1 2  0.2f
        try {
            Double x = Double.parseDouble(str);
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
